<?php

class BasketController extends CustomControllerAction {

    public function init() {
        parent::init();
          if (!$cats = $this->cache->load('leftcat')) {
            // промах кэша
            $cats = DatabaseObject_Categories::GetAllParent($this->db);
            $this->cache->save($cats, 'leftcat');
        } else {
            // попадание в кэш
        }
        $this->view->categories = $cats;
    }

    public function indexAction() {
        $this->view->page_title = "Корзина";
        $status = 'cart';
        if (!isset(Zend_Auth::getInstance()->getIdentity()->id))
        { 
            $basket = DatabaseObject_Basket::GetUserSessionBascket($this->db, $status);
      //    $this->_redirect($this->getUrl('login', 'account'));
        } else
        {
        if (isset(Zend_Auth::getInstance()->getIdentity()->id))
            $basket = DatabaseObject_Basket::GetUserBascket($this->db, Zend_Auth::getInstance()->getIdentity()->id, $status);
        $user = new DatabaseObject_User($this->db);
        $user->load(Zend_Auth::getInstance()->getIdentity()->id);
        $this->view->user = $user;
        // echo '<pre>';print_r($user);echo '</pre>';
       
        }
          $this->view->baskets = $basket; 
       
          
    }

    public function checkoutAction() {
        $this->view->page_title = "Оформление заказа";
        $status = 'cart';
        if(Zend_Auth::getInstance()->getIdentity()->id){
        $address = $this->getRequest()->getParam('address');
        $shopId = $this->getRequest()->getParam('shopId');
        $pc = $this->getRequest()->getParam('pc');
        $pc2 = $this->getRequest()->getParam('pc1');

        if (Zend_Auth::getInstance()->getIdentity()->id)
            $basket = DatabaseObject_Basket::GetProductShopUser($this->db, Zend_Auth::getInstance()->getIdentity()->id, $shopId);

//        echo '<pre>';print_r($basket);echo '</pre>';
        $shop = new DatabaseObject_User($this->db);
        $shop->load($shopId);
        $this->view->shop = $shop;
        
        $user = new DatabaseObject_User($this->db);
        $user->load(Zend_Auth::getInstance()->getIdentity()->id);
        $this->view->user = $user;
        
        $this->view->address = $address;
        $this->view->pc = $pc;
        $this->view->pc2 = $pc2;
        $this->view->baskets = $basket;}
        else
        {
              $this->_redirect($this->getUrl('message'));
        }
    }

    public function confermAction() {
        $this->view->page_title = "Оформление заказа";
        $status = 'cart';
        $city = $this->getRequest()->getParam('city');
        $zip = $this->getRequest()->getParam('zip');
        $address = $this->getRequest()->getParam('address');
        $recipient = $this->getRequest()->getParam('recipient');
        $phone = $this->getRequest()->getParam('phone');
        $shopId = $this->getRequest()->getParam('master_id');
        $pc = $this->getRequest()->getParam('deliverymethod_id');
        $pc2 = $this->getRequest()->getParam('paymethod_id');

        if (Zend_Auth::getInstance()->getIdentity()->id)
            $basket = DatabaseObject_Basket::GetProductShopUser($this->db, Zend_Auth::getInstance()->getIdentity()->id, $shopId);

      //   echo '<pre>';print_r($basket);echo '</pre>';
        $shop = new DatabaseObject_User($this->db);
        $shop->load($shopId);
        $this->view->city = $city;
        $this->view->zip = $zip;
        $this->view->address = $address;
        $this->view->recipient = $recipient;
        $this->view->phone = $phone;
        $this->view->shop = $shop;
        $this->view->address = $address;
        $this->view->pc = $pc;
        $this->view->pc2 = $pc2;
        $this->view->baskets = $basket;
    }

    public function orderAction() {
        $this->view->page_title = "Оформление заказа";
        $order = new DatabaseObject_Orders($this->db);
        $order->userId = Zend_Auth::getInstance()->getIdentity()->id;
        $order->shopId = $this->getRequest()->getParam('master_id');
        $order->price = $this->getRequest()->getParam('price');
        $order->status = 'active';
        $order->date = date("Y-m-d H:i:s");
        $order->city = $this->getRequest()->getParam('city');
        $order->zipcode = $this->getRequest()->getParam('zip');
        $order->address = $this->getRequest()->getParam('address');
        $order->fio = $this->getRequest()->getParam('recipient');
        $order->phone = $this->getRequest()->getParam('phone');
        $order->pc = $this->getRequest()->getParam('paymethod_id');
        $order->pc2 = $this->getRequest()->getParam('deliverymethod_id');
        $order->currency = Zend_Auth::getInstance()->getIdentity()->currency;
        $order->save();
        
        // отправим уведомление о новом заказе
        $userNotificationConfig = new DatabaseObject_NotificationSettings($this->db);
        $userNotificationConfig->load($this->getRequest()->getParam('master_id'), 'idUser');
        
        if($userNotificationConfig && $userNotificationConfig->new_order == 1) {
            DatabaseObject_User::SendNotificationEmail($this->db, 'new_order', $this->getRequest()->getParam('master_id'));
        }
        
        $id = $order->getId();
        
        if ($this->getRequest()->getParam('master_id'))
        {
            
            $user = new DatabaseObject_User($this->db);
            $user->load(Zend_Auth::getInstance()->getIdentity()->id);
            
            $shop = new DatabaseObject_User($this->db);
            $shop->load($this->getRequest()->getParam('master_id'));
            if ($shop->shop->payflag>0)
            {
                $text = "Был получен заказ на сайте http://".$shop->nameshop.".dabahm.com/ Контакты покупателя:".$user->phone.", ".$user->email."";
                if ($shop->phone)
                CustomControllerAction::sendSMS($text,$shop->phone);
            }
        }

        $basket = DatabaseObject_Basket::GetProductShopUser($this->db, Zend_Auth::getInstance()->getIdentity()->id, $this->getRequest()->getParam('master_id'));

        foreach ($basket[products] as $k => $v) {
            $prodId = $v->getId();
            $c=  $basket[count][$k];
            $this->db->query("INSERT INTO  `order_products` (`orderid` ,`product_id`,`count`)VALUES ( '$id', '$prodId', $c);");
        }
        DatabaseObject_Basket::DeleteShopBascket($this->db, $order->shopId, $order->userId);
        $this->_redirect($this->getUrl('complite'));
    }

    public function compliteAction() {
        $this->view->page_title = "Заказ оформлен";
        $this->view->widget = '1';  
        $this->view->basket = DatabaseObject_Basket::GetCountUserBascket($this->db, Zend_Auth::getInstance()->getIdentity()->id, 'cart');
    }
    public function messageAction() {
        $this->view->page_title = "Оформление заказа";
        $this->view->widget = '1';
    }

    public function updateAction() {
         $id = $this->getRequest()->getParam('id');
        $count = $this->getRequest()->getParam('count');
        if (Zend_Auth::getInstance()->getIdentity()->id)
        { 

        DatabaseObject_Basket::SetProductCount($this->db, $id, $count);
        } else 
        {
          $shop =  new DatabaseObject_Products($this->db);
          $shop->load($id);
          $shopId = $shop->userId;
          $_SESSION['cart'][$shopId][$id] = $count;
        }
        echo 'ok';
        exit;
    }

    public function deviationAction() {
        $status = 'deviation';
        $basket = DatabaseObject_Basket::GetUserBascket($this->db, Zend_Auth::getInstance()->getIdentity()->id, $status);
        // echo '<pre>';print_r($basket);echo '</pre>';
        $this->view->baskets = $basket;
    }

    public function finishedAction() {
        $status = 'finished';
        $basket = DatabaseObject_Basket::GetUserBascket($this->db, Zend_Auth::getInstance()->getIdentity()->id, $status);
        // echo '<pre>';print_r($basket);echo '</pre>';
        $this->view->baskets = $basket;
    }

    public function allAction() {
        $status = array('active', 'deviation', 'finished');
        $basket = DatabaseObject_Basket::GetUserBascket($this->db, Zend_Auth::getInstance()->getIdentity()->id, $status);
        // echo '<pre>';print_r($basket);echo '</pre>';
        $this->view->baskets = $basket;
    }

    public function favoriteAction() {
        $status = 'wish';
        $basket = DatabaseObject_Basket::GetUserBascket($this->db, Zend_Auth::getInstance()->getIdentity()->id, $status);
        // echo '<pre>';print_r($basket);echo '</pre>';
        $this->view->baskets = $basket;
    }

    public function deleteitemAction() {
        $id = $this->getRequest()->getParam('id');
        $status = $this->getRequest()->getParam('status');
        $tmp = $this->getRequest()->getParam('callback');
        if (isset(Zend_Auth::getInstance()->getIdentity()->id))
        { 
        $basket = DatabaseObject_Basket::DeleteItemBascket($this->db, $id, Zend_Auth::getInstance()->getIdentity()->id, $status);
        
        $cart = DatabaseObject_Basket::GetCart($this->db, Zend_Auth::getInstance()->getIdentity()->id, 'cart');
        }
        else
        {
          $shop =  new DatabaseObject_Products($this->db);
          $shop->load($id);
          $shopId = $shop->userId;
          unset($_SESSION['cart'][$shopId][$id]);
            $cart = CustomControllerAction::getCart('cart');
        }
        $st = $tmp . '(' . json_encode($cart) . ')';
        echo $st;
        exit;
    }

    public function deleteshopAction() {
        $id = $this->getRequest()->getParam('id');
        $status = $this->getRequest()->getParam('status');
          if (Zend_Auth::getInstance()->getIdentity()->id)
        { 
        $basket = DatabaseObject_Basket::DeleteShopBascket($this->db, $id, Zend_Auth::getInstance()->getIdentity()->id, $status);
        $tmp = $this->getRequest()->getParam('callback');
        $cart = DatabaseObject_Basket::GetCart($this->db, Zend_Auth::getInstance()->getIdentity()->id, 'cart');
        } else
        {

          unset($_SESSION['cart'][$id]);
            $cart = CustomControllerAction::getCart('cart');
        }
        $st = $tmp . '(' . json_encode($cart) . ')';
        echo $st;
        exit;
    }

    public function addbasketAction() {
        $id = $this->getRequest()->getParam('id');

        $status = $this->getRequest()->getParam('status');
        if (isset(Zend_Auth::getInstance()->getIdentity()->id)) {
            CustomControllerAction::SessionFormDb($id);
        /*    $tmp = DatabaseObject_Basket::GetCountProductBascket($this->db, $id, Zend_Auth::getInstance()->getIdentity()->id, $status);

            if ($tmp) {
                $basket = new DatabaseObject_Basket($this->db);
                $basket->load($tmp);
                $basket->count = $basket->count + 1;
                $basket->save();
            } else {
                $basket = new DatabaseObject_Basket($this->db);

                $shop = new DatabaseObject_Products($this->db);
                $shop->load($id);
                $basket->userId = Zend_Auth::getInstance()->getIdentity()->id;
                $basket->shopId = $shop->userId;
                $basket->productId = $id;
                $basket->status = $status;
                $basket->count = 1;
                $basket->save();
            }*/
            $tmp = $this->getRequest()->getParam('callback');
            $cart = DatabaseObject_Basket::GetCart($this->db, Zend_Auth::getInstance()->getIdentity()->id, 'cart');
            $st = $tmp . '(' . json_encode($cart) . ')';
            echo $st;
            exit;
        } else {

            $shop = new DatabaseObject_Products($this->db);
            $shop->load($id);
            $temp = array("$id"=>"1");  
            if (!isset($_SESSION['cart'][$shop->userId][$id])) 
            $_SESSION['cart'][$shop->userId][$id] = 1;
            else
               $_SESSION['cart'][$shop->userId][$id] =    $_SESSION['cart'][$shop->userId][$id] + 1;
            $tmp = $this->getRequest()->getParam('callback');
            $cart = CustomControllerAction::getCart('cart');
            $st = $tmp . '(' . json_encode($cart) . ')';
            echo $st;
            exit;
        }
    }

}

?>
